﻿using System;
using System.Collections.Generic;
using Assets.Scripts.Net;
using Assets.Scripts.Net.Data;
using Assets.Scripts.Net.Data.Model;
using Assets.Scripts.Tool;
using UnityEngine;
using UnityEngine.UI;

namespace Assets.Scripts.UI.Panel
{
    /// <summary>
    /// 考生守则的panel
    /// </summary>
    public class ExamRulePanel:UIBase<ExamPanel>
    {
        public Text TimeText;//时间
        public Text RuleText;//考试规则
        public Text ExamNameText;//考试的名称
        public Button ExitButton;//退出按钮

        private bool isShowTipInfo;//是否显示提示信息
        private ExamInfo examInfo { get; set; }//考试的信息
        private float countDown { get; set; }//考试的倒计时  
        private float waitTime { get; set; }//显示提示界面后的等待时间
        private bool beginWait { get; set; }//开始等待

        protected override UIType UIType
        {
            get { return UIType.Panel;}
        }

        protected override void InitOnAwake()
        {
            AutoFindComponent(transform,true);
            ExitButton.onClick.AddListener(Exit);
        }

        protected override void Update()
        {            
            if (countDown>=0)
            {
                countDown -= Time.deltaTime;
                TimeText.text = TimerTool.GetTime03(countDown);
            }  
            else if (!isShowTipInfo)
            {
                beginWait = true;                
                TimeText.text = TimerTool.GetTime03(0);
                isShowTipInfo = true;
                TipPanel panel= CreatePanel(PanelType.TipPanel).GetComponent<TipPanel>();
                panel.CancleButton.gameObject.SetActive(false);                
                panel.Init("考试已经开始,请点击确定 进入考试!!!", () =>
                {
                    Dictionary<string, object> dict = NetManager.Instance.GetDict();
                    NetManager.Instance.SendRequest(RequestType.DoExam, dict, EnterExamPanel, msg =>
                    {
                        if (msg == "1004")
                        {                          
                            CreatePanel(PanelType.LoginPanel);
                            TipPanel tipPanel = CreatePanel(PanelType.TipPanel).GetComponent<TipPanel>();
                            tipPanel.Init("用户异地登入，请重新登入");
                            Destroy(this.gameObject);
                        }
                        else
                        {
                            isShowTipInfo = false;
                        }
                    });    
                });
            }
            if (beginWait)
            {
                waitTime += Time.deltaTime;
            }
        }
       

        private void EnterExamPanel()
        {
            Exit();
            List<QueInfo> queInfos = NetReaderFactory.Instance().GetInfoByType<List<QueInfo>>() as List<QueInfo>;
            List<AnsInfo> ansInfos = NetReaderFactory.Instance().GetInfoByType<List<AnsInfo>>() as List<AnsInfo>;
            ExamPanel panel = CreatePanel(PanelType.ExamPanel).GetComponent<ExamPanel>();
            int examTime = countDown > 0 ? examInfo.examtime / 1000 - Mathf.RoundToInt(countDown) : examInfo.examtime / 1000 - Mathf.RoundToInt(waitTime);
            panel.Init(ExamType.Exam, examInfo.examname, examTime, queInfos, ansInfos,examInfo.examid);
        }

        public void Init(ExamInfo info)
        {
            examInfo = info;
            countDown = info.countdown/1000;
            //if (countDown <= 0)
            //{
            //    TipPanel panel = CreatePanel(PanelType.TipPanel).GetComponent<TipPanel>();
            //    panel.Init("考试时间已过，请申请补考！！！");
            //    Exit();
            //    return;
            //}
            //RuleText.text = info.rules;
            ExamNameText.text = "距离【" + info.examname + "】还有：";
        }
    }
}